﻿package ${config.packageName}<#if table.packageName?exists>${table.packageName}</#if>${entity.packageName};

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.ronguan.base.service.DefaultBaseService;
import ${config.packageName}<#if table.packageName?exists>${table.packageName}</#if>.dao.I${table.entityName}Dao;
import ${config.packageName}<#if table.packageName?exists>${table.packageName}</#if>.model.${table.entityName};
import ${config.packageName}<#if table.packageName?exists>${table.packageName}</#if>.service.I${table.entityName}Service;
<#if table.tree?exists>
import cn.ronguan.common.utils.StringUtils;
import cn.ronguan.base.model.RequestModel;
import cn.ronguan.pub.service.IParentDataService;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
</#if>

/**
 * 类描述：${table.comment}业务实现类
 * 此类由IKForm平台自定义业务对象生成
 * @author Inkcar<inkcaridge@qq.com>
 * @since 1.0
 */
 @Service("${table.entityName}<#if entity.suffix?exists>${entity.suffix}</#if>")
public class ${table.entityName}<#if entity.suffix?exists>${entity.suffix}</#if> extends DefaultBaseService<${table.entityName}> implements I${table.entityName}Service{
	/**
	 * 注入员工业务数据操作类
	 */
	@Resource(name = "${table.entityName}Dao")
	private I${table.entityName}Dao dao;

	/**
	 * @return the dao
	 */
	public I${table.entityName}Dao getDao() {
		return dao;
	}
	/**
	 * 获取泛型实体实例对象
	 * 
	 * @return
	 */
	public ${table.entityName} getEntityInstance(){
        return ${table.entityName}.newInstance();
    }
    
	/**
	 * 获取实体对象类名称
	 * @return String
	 */
	public String getEntityName(){
        return "${table.entityName}";
    }
    
	/**
	 * 获取实体对象主键属性名称
	 * @return String
	 */
	public String getEntityKeyField(){
        return "${table.primaryProperty.property}";
    }
    
    /**
    * 获取${table.comment}业务实体对象主键值
    */
    public ${table.primaryProperty.propertyType} getEntityKeyId(${table.entityName} bean){
        if(bean==null){
            return null;
        }
        return bean.get${table.primaryProperty.property?cap_first}();
    }
    
    /**
    * 获取${table.comment}业务实体对象主键值
    */
    public void setEntityKeyId(${table.entityName} bean,Object keyValue){
        if(bean==null){
            return;
        }
        <#if table.primaryProperty.propertyType?exists && table.primaryProperty.propertyType!='String'>
        bean.set${table.primaryProperty.property?cap_first}(${table.primaryProperty.propertyType}.valueOf(keyValue.toString()));
        <#else>
        bean.set${table.primaryProperty.property?cap_first}(keyValue.toString());
        </#if>
    }
    <#if table.tree?exists>
	/**
	 * 注入层次路径业务操作类
	 */
	@Resource(name = "ParentDataService")
	private IParentDataService parentDataService;

	/**
	 * @param parentDataService
	 *            the parentDataService to set
	 */
	public void setParentDataService(IParentDataService parentDataService) {
		this.parentDataService = parentDataService;
	}
    
	/**
	 * 数据保存前自定义业务逻辑实现
	 * 
	 * @param tmpBean
	 *            数据实体对象
	 * @param inserted
	 *            是否为新添加
	 * @param model
	 *            请求模型对象
	 * @return boolean,为false时，将不进行数据保存操作
	 */
	@Override
	public boolean saveEntityBefore(${table.entityName} bean, boolean inserted,
			RequestModel model) {
		// 只有修改了parentId属性时才需要设置全路径
		if (!StringUtils.isEmpty(bean.get${table.tree.parentId?cap_first}())) {

			// 组装机构全路径及名称
			List<String> full = parentDataService
					.assembleFullPath(
							bean.get${table.tree.aid?cap_first}(),
							StringUtils.getValue(bean.get${table.tree.fullPath?cap_first}(),
									bean.get${table.tree.parentId?cap_first}()), bean.get${table.tree.text?cap_first}(),
							bean.get${table.tree.fullName?cap_first}());
			if (full != null && full.size() > 0) {
				bean.set${table.tree.fullPath?cap_first}(full.get(0));
				bean.set${table.tree.fullName?cap_first}(full.get(1));
			}

		}
		return true;
	}

	/**
	 * 实体数据保存后事件，需要实体业务操作类自实现
	 * 
	 * @param tmpBean
	 *            数据实体对象
	 * @param json
	 *            数据实体JSON对象,方便方法内容添加响应数据
	 * @param inserted
	 *            是否为新添加
	 * @param model
	 *            请求模型对象
	 */
	@Override
	public void saveEntityAfter(${table.entityName} bean, JSONObject json,
			boolean inserted, RequestModel model) {
		if (StringUtils.isEmpty(bean.get${table.tree.parentId?cap_first}())) {
			return;
		}

		// 创建新层次关系数据，将删除之前失效的层次关系
		parentDataService.insertParentData(bean.get${table.tree.aid?cap_first}(), "${table.entityName}",
				bean.get${table.tree.fullPath?cap_first}(), inserted, model);
	}
    </#if>
    <#if table.sequences?exists>
	/**
	 * 插入新的实体对象到数据库
	 */
	public int insert(${table.entityName} bean) {
		if (bean == null) {
			return 0;
		}
        <#list table.sequences as seq>
        String ${seq.sequenceCode}= this.renderSequences(this.getEntityName() + ".${seq.sequenceCode}", bean);
        <#if seq.propertyType=="String">
        bean.set${seq.property?cap_first}(${seq.sequenceCode});
        <#else>
        bean.set${seq.property?cap_first}(${seq.propertyType}.valueOf(${seq.sequenceCode}));
        </#if>
        </#list>
		return getDao().insert(bean);
	}
    </#if>
}